package com.example.quartz.controller;

import com.example.quartz.service.QuartzService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Quartz 控制器，提供API接口用于管理定时任务
 */
@RestController
@RequestMapping("/api/quartz")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;

    /**
     * 创建一次性任务
     */
    @PostMapping("/create-one-time-job")
    public ResponseEntity<Map<String, String>> createOneTimeJob() {
        try {
            String jobName = quartzService.createOneTimeJob();
            Map<String, String> response = new HashMap<>();
            response.put("message", "一次性任务创建成功");
            response.put("jobName", jobName);
            response.put("jobGroup", "ONE_TIME_GROUP");
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "创建任务失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }

    /**
     * 创建重复执行的任务
     */
    @PostMapping("/create-repeating-job")
    public ResponseEntity<Map<String, String>> createRepeatingJob(
            @RequestParam(defaultValue = "10") int intervalSeconds) {
        try {
            String jobName = quartzService.createRepeatingJob(intervalSeconds);
            Map<String, String> response = new HashMap<>();
            response.put("message", "重复任务创建成功");
            response.put("jobName", jobName);
            response.put("jobGroup", "REPEATING_GROUP");
            response.put("intervalSeconds", String.valueOf(intervalSeconds));
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "创建任务失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }

    /**
     * 检查任务是否存在
     */
    @GetMapping("/check-job")
    public ResponseEntity<Map<String, Object>> checkJob(
            @RequestParam String jobName,
            @RequestParam(defaultValue = "ONE_TIME_GROUP") String jobGroup) {
        try {
            boolean exists = quartzService.checkJobExists(jobName, jobGroup);
            Map<String, Object> response = new HashMap<>();
            response.put("jobName", jobName);
            response.put("jobGroup", jobGroup);
            response.put("exists", exists);
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "检查任务失败: " + e.getMessage());
            return ResponseEntity.badRequest().body((Map<String, Object>) (Map<?, ?>) error);
        }
    }

    /**
     * 暂停任务
     */
    @PostMapping("/pause-job")
    public ResponseEntity<Map<String, String>> pauseJob(
            @RequestParam String jobName,
            @RequestParam(defaultValue = "REPEATING_GROUP") String jobGroup) {
        try {
            quartzService.pauseJob(jobName, jobGroup);
            Map<String, String> response = new HashMap<>();
            response.put("message", "任务已暂停");
            response.put("jobName", jobName);
            response.put("jobGroup", jobGroup);
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "暂停任务失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }

    /**
     * 恢复任务
     */
    @PostMapping("/resume-job")
    public ResponseEntity<Map<String, String>> resumeJob(
            @RequestParam String jobName,
            @RequestParam(defaultValue = "REPEATING_GROUP") String jobGroup) {
        try {
            quartzService.resumeJob(jobName, jobGroup);
            Map<String, String> response = new HashMap<>();
            response.put("message", "任务已恢复");
            response.put("jobName", jobName);
            response.put("jobGroup", jobGroup);
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, String> error = new HashMap<>();
            error.put("error", "恢复任务失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(error);
        }
    }

    /**
     * 删除任务
     */
    @PostMapping("/delete-job")
    public ResponseEntity<Map<String, Object>> deleteJob(
            @RequestParam String jobName,
            @RequestParam(defaultValue = "REPEATING_GROUP") String jobGroup) {
        try {
            quartzService.deleteJob(jobName, jobGroup);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "任务已删除");
            return ResponseEntity.ok(response);
        } catch (SchedulerException e) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("error", "删除任务失败");
            error.put("detail", e.getMessage()); // 增加具体错误详情
            return ResponseEntity.status(500).body(error); // 使用500状态码
        }
    }
}
    